package com.evil.common.application.enums;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 发布状态
 *
 * @author ly
 */
@AllArgsConstructor
public enum PublishStatusEnum {

    /**
     * 发布状态
     */
    UN_PUBLISH(0, "有未发布"),
    PUBLISHED(1, "已发布"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<PublishStatusEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static PublishStatusEnum isExistById(Integer id) {
        return PublishStatusEnum.findById(id).orElseThrow(() ->
                new BusinessException(BaseEnum.ERROR_PARAMETER, "发布状态参数错误"));
    }

}
